package com.ruoyi.web.controller.kujiale;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.dto.SubAccountQueryDTO;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.IAccountService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.utils.SysUserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/kujiale/account")
@Api(tags = "账号信息")
public class AccountController {

    @Resource
    private IAccountService accountService;

    @GetMapping("/token")
    @ApiOperation("获取登录信息")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "dest",
                    value = "根据不同商家对不同落地页的需求，我们开放参数dest来控制选择落地页，同时根据dest取值的不同，可能还需要搭配其他参数。默认:0",
                    dataType = "String",
                    paramType = "query",
                    example = "1"
            )})
    public AjaxResult getToken(@RequestParam(required = false, defaultValue = "0") String dest) {
        // 看下用户登录了嘛
        // 从上下文获取当前用户ID
        try {
            String kujialeId = SysUserUtils.getKujialeId();
            if (StringUtils.isBlank(kujialeId)) {
                return AjaxResult.error("没找到你哇");
            }
            String token = accountService.getToken(kujialeId, dest);
            return AjaxResult.success("成了", token);
        } catch (ServiceException e) {
            if (e.getCode() != null && HttpStatus.UNAUTHORIZED == e.getCode()) {
                return AjaxResult.error("好小子，没登录");
            }
            return AjaxResult.error(e.getMessage());
        }
    }

    @PostMapping("/register")
    @ApiOperation("绑定酷家乐账号")
    public AjaxResult register(@RequestParam String userId, // 本系统用户ID
                               @RequestParam String appUid, // 本系统用户ID
                               @RequestParam String userName,
                               @RequestParam String email,
                               @RequestParam String kujialeBindReason) {
        return AjaxResult.success("成了", accountService.register(appUid, userId, userName, email,
                "https://aobayijia.oss-cn-shenzhen.aliyuncs.com/jiazhuang.png",
                0, "moren123", null, kujialeBindReason));
    }

    /**
     * 账号解绑
     */
    @PostMapping("/unbind")
    @ApiOperation("账号解绑")
    public AjaxResult unbind(@RequestParam String userId) {
        boolean result = accountService.unbind(userId);
        return result ? AjaxResult.success("解绑成功") : AjaxResult.error("解绑失败");
    }

    /**
     * 账号换绑
     */
    @PostMapping("/rebind")
    @ApiOperation("账号换绑")
    public AjaxResult rebind(@RequestParam String appUid,
                             @RequestParam String userId,
                             @RequestParam String oldUserId,
                             @RequestParam String kujialeBindReason,
                             @RequestParam String kujialeRebindReason) {
        boolean result = accountService.rebind(appUid, userId,oldUserId, kujialeBindReason, kujialeRebindReason);
        return result ? AjaxResult.success("换绑成功") : AjaxResult.error("换绑失败");
    }

    /**
     * 查询子账号列表
     */
    @GetMapping("/querySubAccountList")
    public AjaxResult querySubAccountList(@RequestParam(required = false) Integer subAccountStatus,
                                         @RequestParam(required = false) Integer isBindSubAccount,
                                         @RequestParam(required = false) String subAccountName,
                                         @RequestParam(required = false) String designerName,
                                         @RequestParam(required = false) String designerPhone,
                                         @RequestParam(required = false, defaultValue = "0") Integer start,
                                         @RequestParam(required = false, defaultValue = "50") Integer num) {
        
        // 构建查询条件DTO
        SubAccountQueryDTO queryDTO = new SubAccountQueryDTO();
        queryDTO.setSubAccountStatus(subAccountStatus);
        queryDTO.setIsBindSubAccount(isBindSubAccount);
        queryDTO.setSubAccountName(subAccountName);
        queryDTO.setDesignerName(designerName);
        queryDTO.setDesignerPhone(designerPhone);
        queryDTO.setStart(start);
        queryDTO.setNum(num);
        
        Object subAccountList = accountService.querySubAccountList(queryDTO);
        return AjaxResult.success("查询成功", subAccountList);
    }

    /**
     * 获取账号上限和数量信息
     */
    @GetMapping("/getAccountLimitInfo")
    @ApiOperation("获取账号上限和数量信息")
    public AjaxResult getAccountLimitInfo(@RequestParam String appUid) {
        Object limitInfo = accountService.getAccountLimitInfo(appUid);
        return AjaxResult.success("查询成功", limitInfo);
    }

    /**
     * 禁用酷家乐账号
     */
    @PostMapping("/disableAccount")
    @ApiOperation("禁用酷家乐账号")
    public AjaxResult disableKujialeAccount(@RequestParam String appUid) {
        try {
            boolean result = accountService.disableKujialeAccount(appUid);
            return result ? AjaxResult.success("账号禁用成功") : AjaxResult.error("账号禁用失败");
        } catch (Exception e) {
            log.error("禁用酷家乐账号失败，appUid: {}", appUid, e);
            return AjaxResult.error("账号禁用失败：" + e.getMessage());
        }
    }

    /**
     * 启用酷家乐账号
     */
    @PostMapping("/enableAccount")
    @ApiOperation("启用酷家乐账号")
    public AjaxResult enableKujialeAccount(@RequestParam String appUid) {
        try {
            boolean result = accountService.enableKujialeAccount(appUid);
            return result ? AjaxResult.success("账号启用成功") : AjaxResult.error("账号启用失败");
        } catch (Exception e) {
            log.error("启用酷家乐账号失败，appUid: {}", appUid, e);
            return AjaxResult.error("账号启用失败：" + e.getMessage());
        }
    }
}
